<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleFilter()"
          type="primary"
          size="small">
          查询结果
        </el-button>
        <el-button
          style="float: right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="元器件名称：">
            <el-input style="width: 203px" v-model="listQuery.name" placeholder="元器件名称"></el-input>
          </el-form-item>
          <el-form-item label="SN：">
            <el-input style="width: 203px" v-model="listQuery.sn" placeholder="SN"></el-input>
          </el-form-item>
          <el-form-item label="所属项目：">
            <el-input style="width: 203px" v-model="listQuery.project" placeholder="所属项目"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="electronicStatus" placeholder="未删除" clearable>
              <el-option label="已删除" value="0"> </el-option>
              <el-option label="未删除" value="1"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时间：">
            <el-date-picker
              v-model="listQuery.datetimeStart"
              type="date"
              placeholder="开始日期">
            </el-date-picker>
            <span class="demonstration">-</span>
            <el-date-picker
              v-model="listQuery.datetimeEnd"
              type="date"
              placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-table
      :data="list"
      v-loading="listLoading"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="名称">
              <span>{{ props.row.name }}</span>
            </el-form-item >
            <el-form-item label="SN">
              <span>{{ props.row.sn }}</span>
            </el-form-item >
            <el-form-item label="封装">
              <span>{{ props.row.package }}</span>
            </el-form-item >
            <el-form-item label="价格">
              <span>{{ props.row.price }}</span>
            </el-form-item >
            <el-form-item label="库存">
              <span>{{ props.row.stock }}</span>
            </el-form-item >
            <el-form-item label="位置">
                <span>{{ props.row.position }}</span>
            </el-form-item >
            <el-form-item label="项目">
                <span>{{ props.row.project }}</span>
            </el-form-item >
            <el-form-item label="供货商">
                <span>{{ props.row.supplier }}</span>
            </el-form-item >
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="出库数"
        prop="count">
      </el-table-column>
      <el-table-column
        label="备注"
        prop="note">
      </el-table-column>
      <el-table-column
        label="出库时间"
        prop="created_time">
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />

  </div>
</template>

<script>

import { electronicInStockLogList } from '@/api/electronic'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'find',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 10,
      selectRow: null,
      listQuery: {
        name: '',
        project: '',
        sn: '',
        status: 1,
        page: 1,
        limit: 20
      },
      electronicStatus: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    dateFormat(time) {
      var date = new Date(time)
      var year = date.getFullYear()
      /* 在日期格式中，月份是从0开始的，因此要加0
       * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
       * */
      var month =
        date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1
      var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      var hours =
        date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      var minutes =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      var seconds =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      // 拼接
      return (
        year +
        '-' +
        month +
        '-' +
        day +
        ' ' +
        hours +
        ':' +
        minutes +
        ':' +
        seconds
      )
    },
    fetchData() {
      this.listLoading = true
      if(this.electronicStatus) {
        this.listQuery.status = this.electronicStatus
      }
      if (this.listQuery.datetimeStart){
        this.listQuery.datetimeStart = this.dateFormat(this.listQuery.datetimeStart)
      }
      if (this.listQuery.datetimeEnd){
        this.listQuery.datetimeEnd = this.dateFormat(this.listQuery.datetimeEnd)
      }
      electronicInStockLogList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.items
        this.total = response.data.total
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
  }
}
</script>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

